package com.zzld.modules.sys.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.entity.Org;
import com.zzld.modules.sys.security.ShiroUser;
import com.zzld.modules.sys.service.IOrgService;

@Controller
@RequestMapping("/sys/org")
public class OrgController {

	@Autowired
	private IOrgService orgService;

	private String listurl = "/modules/sys/org/orgList";
	private String orgcru = "/modules/sys/org/orgCru";

	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest request) {
		return listurl;
	}

	@RequestMapping("update/json")
	public String j(HttpServletRequest requst) {
		return orgcru;
	}

	/*
	 * 删除操作
	 */
	@RequestMapping("update/del")
	@ResponseBody
	public CommonJSON del(HttpServletRequest request, Org o, Model model) {
		CommonJSON j = new CommonJSON();
		try {
			o.setId(request.getParameter("id"));
			if (o.getId() != "") {
				if (orgService.Updel(o, (byte) 0) > 0) {
					j.setMessage(MessageUtils.DELETE_SUCCESS);
					j.setSuccess(true);
				} else {
					j.setMessage("请先删除子节点");
					j.setSuccess(false);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMessage(MessageUtils.DELETE_WARNING);
			j.setSuccess(false);
		}
		return j;
	}

	/*
	 * 新增/修改
	 */
	@RequestMapping(value = "update/json/pre", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(Org o, Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		o.setCreateBy(userq.getName());
		o.setCreateDate(new Date());
		if (StringUtils.isBlank(o.getId())) {
			o.setId(UuidUtil.uuid().toString());// 新增
			try {
				orgService.AddOrg(o);
				json.setMessage(MessageUtils.ADD_SUCCESS);
				json.setSuccess(true);
			} catch (Exception ex) {
				ex.printStackTrace();
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		} else {
			try {
				orgService.Updel(o, (byte) 1);
				json.setMessage(MessageUtils.EDIT_SUCCESS);
				json.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}
		return json;

	}

	@RequestMapping("update/json/pre/sel")
	@ResponseBody
	public Org se(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (id != "") {
			return orgService.Selectbyid(id);
		}
		return null;

	}

	@RequestMapping(value = "treegrid")
	@ResponseBody
	public List<TreeGrid> Tree(HttpServletRequest request, Model model) {
		return orgService.treegrid();
	}

}
